package com.biyabi.controller;

import com.biyabi.entity.Admin;
import com.biyabi.pojo.Result;
import com.biyabi.service.AdminService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/back/admin")
public class AdminController {
    @Resource
    private AdminService adminService;
    @RequestMapping("/login")
    public Result login(Admin admin, HttpServletRequest request){
        Result result = new Result();
        Admin admin1 = adminService.login(admin);
        if(admin1!=null){
            request.getSession().setAttribute("admin",admin1);
            result.setStatus(true);
            result.setMessage(admin1.getRealname());
        }else{
            result.setStatus(false);
            result.setMessage("用户名或密码错误！");
        }
        return result;
    }
    @RequestMapping("/logout")
    public Result logout( HttpServletRequest request){
        Result result = new Result();
        try{
            request.getSession().invalidate();
            result.setStatus(true);
            result.setMessage("退出成功！");
        }catch(Exception e) {
            result.setStatus(false);
            result.setMessage("退出错误！");
        }
        return result;
    }
}
